DECLARE Statement (BASIC Procedures) ---------------------------------------------------------------------------- Action Declares references to BASIC procedures and invokes argument type checking. Syntax DECLARE { FUNCTION | SUB} name -(- parameterlist-)- Remarks The DECLARE statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- name The name that will be used to invoke the procedure. The argument name is limited to 40 characters. FUNCTION procedure names can end in one of the type-declaration characters ( %, &, !, #, @, % Argument Description ---------------------------------------------------------------------------- characters ( %, &, !, #, @, % or $) to indicate the type of value returned. parameterlist A list of parameters that indicates the number and type of arguments to be used to call the procedure. Syntax is shown below. Only the number and type of the arguments are significant. For calls within BASIC, the DECLARE statement is required only if you call SUB procedures without the CALL keyword, or if you invoke a FUNCTION procedure defined in another module. For more information about invoking procedures without the CALL keyword, see Chapter 2, "SUB and FUNCTION Procedures" in the Programmer's Guide. A DECLARE statement also causes the compiler to check the number and type of arguments used to invoke the procedure. QBX automatically generates DECLARE statements when you save your program while working in the environment. The DECLARE statement can appear only in module-level code (not in a SUB or FUNCTION procedure) and affects the entire module. The parameterlist serves as a prototype for checking the number and type of the arguments in SUB and FUNCTION procedure calls. It has the following syntax. variable-AS type--, variable -AS type--... The argument variable is any valid BASIC variable name. If the variable is an array, it can be followed by the number of dimensions in parentheses, as in this fragment. DECLARE SUB DisplayText (A(2) AS STRING) DIM Text$(100,5) . . . CALL DisplayText(Text$()) The number of dimensions is optional. The type is either INTEGER, LONG, SINGLE, DOUBLE, CURRENCY, STRING, or a user-defined type. Again, only the number and types of arguments are significant. Note You cannot have fixed-length strings in DECLARE statements, because only variable-length strings can be passed to SUB and FUNCTION procedures. Fixed-length strings can appear in an argument list but are converted to variable-length strings before being passed. A variable's type also can be indicated by including an explicit type character ( %, &, !, #, @, or $) or by relying on the default type. The form of the parameter list determines whether or not argument checking is done, as shown in the following list. DECLARE SUB First You can omit the parentheses only if the SUB or FUNCTION procedure is separately compiled. No argument checking is done. DECLARE SUB First () First has no parameters. If you use arguments in a call to First, BASIC generates an error. An empty parameter list indicates that the SUB or FUNCTION procedure has no parameters and that argument checking should be done. DECLARE SUB First (X AS LONG) First has one long-integer parameter. The number and type of the arguments in each call or invocation are checked when the parameter list appears in the DECLARE statement. DECLARE also can be used to declare references to procedures written in other programming languages, such as C. See the entry for the DECLARE statement (Non-BASIC). See Also CALL (BASIC), DECLARE (Non-BASIC), FUNCTION, SUB Example In this program, use of the DECLARE statement allows a SUB procedure to be invoked without using the CALL keyword. ' Generate 20 random numbers, store them in an array, and sort. DECLARE SUB Sort (A() AS INTEGER, N AS INTEGER) DIM Array1(1 TO 20) AS INTEGER DIM I AS INTEGER ' Generate 20 random numbers. RANDOMIZE TIMER CLS FOR I% = 1 TO 20 Array1(I%) = INT(RND * 100) NEXT I% ' Sort the array, calling Sort without the CALL keyword. ' Notice the absence of parentheses around the arguments in ' the call to Sort. Sort Array1(), 20 ' Print the sorted array. FOR I% = 1 TO 20 PRINT Array1(I%) NEXT I% END ' Sort subroutine. SUB Sort (A%(), N%) FOR I% = 1 TO N% - 1 FOR J% = I% + 1 TO N% IF A%(I%) > A%(J%) THEN SWAP A%(I%), A%(J%) NEXT J% NEXT I% END SUB